package com.gigmissions.client;


import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.geocode.Placemark;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.ListBox;


public class ChoosePlacemarkContainer extends ListBox {
	
	private JsArray<Placemark> placemarks;
	private MapWidget map;
	private String placemarkName;
	private Button okButton;
		
	
	public ChoosePlacemarkContainer(MapWidget map){
		super(false);
		this.map = map;
	}
		
	public JsArray<Placemark> getPlacemarks() {
		return placemarks;
	}

	public void setPlacemarks(JsArray<Placemark> placemarks) {
		this.placemarks = placemarks;
	}
	
	public void setOkButton(Button okButton) {
		this.okButton = okButton;
	}
	
	
	
	public String getPlacemarkName() {
		return placemarkName;
	}

	public void setPlacemarkName(String placemarkName) {
		this.placemarkName = placemarkName;
	}

	public void addPlacemarks(JsArray<Placemark> placemarks) {
		this.setPlacemarks(placemarks);
		setVisibleItemCount(10);
		for(int i = 0;i<placemarks.length();i++){
			addItem((String) placemarks.get(i).getAddress());
		}

		// Add a handler to handle drop box events
	    addChangeHandler(new ChangeHandler() {
	    	
	      public void onChange(ChangeEvent event) {
	        
	    	   okButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
	    		   @Override
	    		   public void componentSelected(ButtonEvent ce) {
	  				
	    			   Placemark selectedPlacemark = (Placemark)getPlacemarks().get(getSelectedIndex());
	    			   VenueServiceUtil.zoomToAddressAndAddMarker(selectedPlacemark.getAddress(), map, getPlacemarkName());
	    			   
	    			   	  				 
	  				  
	  			}
	  	    	 
	  	    	 
	  	      	});
	    	  
	    	  okButton.setEnabled(true);
	        
	        
	      }
	    });
		
	}

		

	
	
	

}
